import request from '@/utils/request'
import { LOGIN, LOGOUT } from '@/store/actionTypes'
import { removeToken, setToken } from '@/utils/storage'
export const userLogin = (formData) => {
    return  async dispatch => {
        // 发异步请求
        // console.log(' 发异步请求formData',formData)
        const res = await request({
            url: '/authorizations',
            method:'POST' ,
            data: formData  // data | params
        })

        // 持久化 token
        setToken(res.data.token)

        dispatch({
            type: LOGIN,
            payload: res.data.token
        })
    }
}

export const logout = () => {
    return  dispatch => {
        // 清空 本地 token
        removeToken()

        // 清空 redux
        dispatch({
            type: LOGOUT
        })
    }
}